import 'package:flutter/material.dart';
import 'package:summer/navigator/hi_navigator.dart';
import 'package:summer/pages/login_register/page/register_page.dart';
import 'package:uikit/http/core/hi_net_error.dart';
import 'package:uikit/http/dao/login_dao.dart';
import 'package:uikit/utils/toast_util.dart';

import 'login_base_view_model.dart';

class LoginViewModel extends LoginBaseViewModel {
  @override
  void sendUserRequest(String userName, String password, {String? imoocId, String? orderId}) {
    sendLoginRequest(userName, password);
  }

  /// 发送登录请求
  void sendLoginRequest(String userName, String password) async {
    try {
      var result = await LoginDao.login(userName, password);
      print("sendLoginRequest result = $result");
      if (result['code'] == 0) {
        print('登录成功');
        ToastUtil.show("登录成功");
        Navigator.of(context!).pushNamed("/Register");
        //showToast('注册成功');
        // if (widget.onJumpToLogin != null) {
        //   widget.onJumpToLogin();
        // }
      } else {
        print(result['msg']);
        //showWarnToast(result['msg']);
      }
    } on NeedAuth catch (e) {
      //ToastUtil.show(e.message);
      print("sendLoginRequest NeedAuth = $e");
      HiNavigator.getInstance().onJumpTo(RouteStatus.home);
      //showWarnToast(e.message);
    } on HiNetError catch (e) {
      ToastUtil.show(e.message);
      print("sendLoginRequest HiNetError = $e");
      //showWarnToast(e.message);
    }
  }
}
